package assign1;

/**
 *
 * @author kevin louis
 */
import java.util.ArrayList;
import java.util.Random;
/** grid class*/
public class GridView {

    private char[][] cell;
    private ProjModel pm;
    private int gridSize;
    private trafficLight tl;
    private ArrayList<Cars> carsVer;
    private ArrayList<Cars> carsHor;
    private int k = 0;

/**grid constructor*/
    public GridView(ProjModel pm) {
        this.pm = pm;
        cell = new char[pm.getGridsize()][pm.getGridsize()];
        gridSize = pm.getGridsize();
        carsVer = new ArrayList<Cars>();
        carsHor = new ArrayList<Cars>();
        tl = new trafficLight();
    }
   /**initiate cell with "="*/
    public void initiGrid(char ch) {

        for (int i = 0; i < cell.length; i++) {
            for (int j = 0; j < cell.length; j++) {
                cell[i][j] = ch;
            }
        }
    }
  /**build the road*/
    public void buildRoad() {
        buildHorLanes();
        buildVerLanes();
        //horStripe(pm.gethorLanes());
        //verStripe(pm.getverLanes());

    }
  /**class to run the simulation*/
    public void runSim(ProjModel pm, int cycle) {
        pm.setCycleNum(cycle);
        int count = pm.getCycleNum();
        for (; k < count; k++) {
            buildCarsHor(pm.getHorProb(), pm.gethorLanes());
            buildCarsVer(pm.getVerProb(), pm.getverLanes());
            tl.trafficRun(k);
            trafficSim(count);
            showGrid();
            System.out.println(" ");
        }
        //horStripe(pm.gethorLanes());
        //verStripe(pm.getverLanes());
        //buildCarsVer();

    }
    /**class to show the grid*/
    public void showGrid() {
        for (int i = 0; i < cell.length; i++) {
            for (int j = 0; j < cell.length; j++) {
                System.out.print(cell[i][j]);
            }
            System.out.println();
        }
    }
/**class to build the horizontal lanes*/
    public void buildHorLanes() {
        int middle = gridSize / 2;
        /**if user set horLane=3*/
        int horLane = pm.gethorLanes();//horLanes=3
        for (int i = 0; i < gridSize; i++) {
            for (int c = 0; c < horLane; c++) {
                cell[middle + c][i] = ' ';
            }
        }
    }
/**class to build the vertical lanes*/
    public void buildVerLanes() {
        int middle = gridSize / 2;//if user set verLane=3
        int verLane = pm.getverLanes();
        for (int i = 0; i < gridSize; i++) {
            for (int c = 0; c < verLane; c++) {
                cell[i][middle + c] = ' ';
            }
        }
    }
//
//    public void horStripe(int horLane) {
//
//        int upperStripe = cell.length / 2 - horLane;
//        int lowerStripe = cell.length / 2 + horLane / 2;
//
//        if (horLane == 1) {
//            lowerStripe = cell.length / 2 + horLane;
//        }
//        for (int s = 0; s < cell.length; s++) {
//            char[] stripe = cell[s];
//            for (int z = 0; z < stripe.length; z++) {
//                if (s == upperStripe) {
//                    stripe[z] = '-';
//                }
//                if (s == lowerStripe) {
//                    stripe[z] = '-';
//                }
//            }
//        }
//    }
//
//    public void verStripe(int verLane) {
//
//        int upperStripe = cell.length / 2 + verLane / 2;
//        int lowerStripe = cell.length / 2 - verLane;
//
//        if (verLane == 1) {
//            upperStripe = cell.length / 2 + verLane;
//        }
//        if (verLane == 4) {
//            lowerStripe = lowerStripe + 1;
//        }
//        for (int s = 0; s < cell.length; s++) {
//            char[] stripe = cell[s];
//            for (int z = 0; z < stripe.length; z++) {
//                if (z == upperStripe) {
//                    stripe[z] = '|';
//                }
//                if (z == lowerStripe) {
//                    stripe[z] = '|';
//                }
//            }
//        }
//    }
/**class to build car(probability) for horizontal lane*/
    public void buildCarsHor(double horProb, int horLane) {
        Random rndm = new Random();
        float x = rndm.nextFloat();

        if (horProb >= x) {
            if (horLane == 1) {
                carsHor.add(new Cars(12, 0));
            }
            if (horLane == 2) {
                carsHor.add(new Cars((12 + (int) (Math.random() * 2)), 0));
            }
            if (horLane == 3) {
                carsHor.add(new Cars((12 + (int) (Math.random() * 3)), 0));
            }
        }
    }
/**class to build car(probability) for vertical lane*/
    public void buildCarsVer(double verProb, int verLane) {
        Random rndm = new Random();
        float y = rndm.nextFloat();

        if (verProb >= y) {
            if (verLane == 1) {
                carsVer.add(new Cars(0, 12));
            }
            if (verLane == 2) {
                carsVer.add(new Cars(0, (12 + (int) (Math.random() * 1))));
            }
            if (verLane == 3) {
                carsVer.add(new Cars(0, (12 + (int) (Math.random() * 2))));
            }
            if (verLane == 4) {
                carsVer.add(new Cars(0, (13 + (int) (Math.random() * 3))));
            }
        }
    }
/**class to show the car(vertical)*/
    public void showCarsVer() {
        for (int i = 0; i < carsVer.size(); i++) {
            int setX = carsVer.get(i).getXPos();
            int setY = carsVer.get(i).getYPos();
            cell[setX][setY] = 'c';
        }
    }
/**class to show the car(horizontal)*/
    public void showCarsHor() {
        for (int i = 0; i < carsHor.size(); i++) {
            int setX = carsHor.get(i).getXPos();
            int setY = carsHor.get(i).getYPos();
            cell[setX][setY] = 'c';
        }
    }
/**class to run and delete the cars*/
    public void trafficSim(int cycleNum) {
        if (cycleNum == 1) {
            showCarsHor();
            showCarsVer();
        } else if (cycleNum > 1) {
            for (int count = 0; count < carsHor.size(); count++) {
                if (tl.getHorRed() == true) {
                    if (carsHor.get(count).getXPos() < 10) {
                        delTailHor(count);
                        moveCarsHor(count);
                    } else if (carsHor.get(count).getXPos() > 10) {
                        delTailHor(count);
                        moveCarsHor(count);
                    }
                } else if (tl.getHorRed() == false) {
                    delTailHor(count);
                    moveCarsHor(count);
                }
            }
            for (int count = 0; count < carsVer.size(); count++) {
                if (tl.getVerRed() == true) {
                    if (carsVer.get(count).getYPos() < 10) {
                        delTailVer(count);
                        moveCarsVer(count);
                    }
                    if (carsVer.get(count).getYPos() > 10) {
                        delTailVer(count);
                        moveCarsVer(count);
                    }
                } else if (tl.getVerRed() == false) {
                    delTailVer(count);
                    moveCarsVer(count);
                }
            }

        }
    }
/**class to move horizontal cars*/
    public void moveCarsHor(int sim) {
        carsHor.get(sim).setYPos((carsHor.get(sim).getYPos()) + 1);

        int x = carsHor.get(sim).getXPos();
        int y = carsHor.get(sim).getYPos();
        if (y > 23) {
            carsHor.remove(sim);
        } else {
            cell[x][y] = 'c';
        }

    }
/**class to move the vertical cars*/
    public void moveCarsVer(int sim) {
        carsVer.get(sim).setXPos((carsVer.get(sim).getXPos()) + 1);
        int x = carsVer.get(sim).getXPos();
        int y = carsVer.get(sim).getYPos();
        if (x > 23) {
            carsVer.remove(sim);
        } else {
            cell[x][y] = 'c';
        }
    }
/**class to delete car in horizontal lane*/
    public void delTailHor(int track) {
        int formerX = carsHor.get(track).getXPos();
        int formerY = carsHor.get(track).getYPos();
        cell[formerX][formerY] = ' ';
    }
/** class to delete car in vertical lane*/
    public void delTailVer(int track) {
        int formerX = carsVer.get(track).getXPos();
        int formerY = carsVer.get(track).getYPos();
        cell[formerX][formerY] = ' ';
    }
    //public void buildCarsVer(){
    //  int middle=gridSize/2;
    //carsArray.add(new Cars())
}